<?php

class Acceso_Controller_Plugin_Param extends Zend_Controller_Plugin_Abstract {

    protected $_param;
    private $_userID;

    public function preDispatch(Zend_Controller_Request_Abstract $request) {
        $auth = Zend_Auth::getInstance();
        $frontController = Zend_Controller_Front::getInstance();
        $dispatcher = $frontController->getDispatcher();
        $request = $this->getRequest();
        $module = $request->getModuleName();
        $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector');
        $controllerName = $request->getControllerName();
        $action = $request->getActionName();
        $acl = new Acl_Acceso();

        if ($module == 'default')
            $module = 'admin';
        if ($auth->hasIdentity()) {
            
            if ($action == 'error' or $controllerName == 'error') {
                $r = $this->getResponse()->getException();
                $code = $r[0]->getCode();
                if ($code == 404) {
                    $redirector->gotoUrlAndExit('/admin/index/index/msg/1');
                }
            }
            $user_data = new Groups_Model_DbTable_Groups();
            $user = $user_data->getName($auth->getIdentity()->idgroup);
            if (!$dispatcher->isDispatchable($request)) {
                $redirector->gotoUrlAndExit('/admin/index/index/msg/1');
            }

            if (!$acl->has($module)) {
                $redirector->gotoUrlAndExit('/admin/index/index/msg/1');
            }

            if (!$acl->isAllowed($user, $module, $controllerName . '/' . $action)) {
                $redirector->gotoUrlAndExit('/admin/index/index/msg/1');
            }
        } else {
            $user = 'guest';
            if (!$acl->has($module)) {
                if (!$dispatcher->isDispatchable($request)) {
                    $redirector->gotoUrlAndExit('/admin/admin/login');
                }
                $p = 'mod/' . $request->getParam('module') . '/';
                $p .= 'cont/' . $request->getParam('controller') . '/';
                $p .= 'act/' . $request->getParam('action');
            
                foreach ($request->getParams() as $key => $value) {
                    if($key != 'module' && $key != 'controller' && $key != 'action'){
                        $p .= '/' . $key . '/' . $value;
                    }
                }

                $redirector->gotoUrlAndExit('/admin/admin/login/' . $p );
            }
            if (!$dispatcher->isDispatchable($request)) {
                $redirector->gotoUrlAndExit('/admin/admin/login');
            }
            if (!$acl->isAllowed($user, $module, $controllerName . '/' . $action)) {
                $p = 'mod/' . $request->getParam('module') . '/';
                $p .= 'cont/' . $request->getParam('controller') . '/';
                $p .= 'act/' . $request->getParam('action');
                $redirector->gotoUrlAndExit('/admin/admin/login/' . $p);
            }
        }
    }

}

?>
